/*
 * copyright (c) 2009 Alexandr [Sn@ble] Surnin
 *
 * This file is part of FrameSeeker.
 *
 * FrameSeeker is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FrameSeeker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You can read the full version of GNU LGPL in
 * http://www.gnu.org/copyleft/lesser.html
 */


#include "FSCore.h"

#include "FSDefines.h"

#include <QDebug>



FSCore::FSCore() {

    ffmpeg = new FSffmpeg();

    // Load methods and settings

    readSettings();

    // Direct ffmpeg output to "methods"
    foreach (FSCoreMethod *method, methods) {
        QObject::connect(ffmpeg, SIGNAL(sendFrame(FSFrame*)),
                         method, SLOT(setFrame(FSFrame*)));
    }
}


int FSCore::process() {

    quint64 status = ffmpeg->openFile(fileName);
    if (status != FS_SUCCESS) {
        qDebug() << "File cannot be open";
        return status;
    }

    while (true) {
        if (ffmpeg->readFrame() == FS_VIDEO_ENDS) return FS_SUCCESS;

    }

    return FS_SUCCESS;
}


int FSCore::process( const QString &_fileName ) {

    setFile(_fileName);
    process();
    return FS_SUCCESS;
}


int FSCore::stop() {

    return FS_SUCCESS;
}


void FSCore::setFile( const QString &_fileName ) {

    fileName = _fileName;
}


void FSCore::loadMethods() {

    if (method.contains("Histograms")) {
        FSMethodHist *methodHist = new FSMethodHist();
        methods << methodHist;
    }

    if (method.contains("Motion")) {
        FSMethodMV *methodMV = new FSMethodMV();
        methods << methodMV;
    }
}


void FSCore::readSettings() {

    settings = new QSettings("frameSeeker.conf", QSettings::IniFormat);
    method = settings->value("Method/Name").toString();

    FSCoreMethod::setTestingMode(settings->value("Method/Testing_mode").toBool());

    ffmpeg->readSettings(settings);
    FSFrame::readSettings(settings);

    loadMethods();

    // Read methods settings
    foreach (FSCoreMethod *_method, methods) {
        _method->readSettings(settings);
    }

}
